package cn.iocoder.devlop.module.annual.report.socsecinfo.service.socsecinfo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.devlop.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.devlop.module.annual.report.baseinfo.dal.dataobject.BaseinfoDO;
import cn.iocoder.devlop.module.annual.report.baseinfo.service.BaseinfoService;
import cn.iocoder.devlop.module.annual.report.common.service.EnttypeEntclassifyService;
import cn.iocoder.devlop.module.annual.report.marpripinfo.service.MarpripinfoService;
import cn.iocoder.devlop.module.annual.report.socsecinfo.controller.vo.SocsecinfoCreateReqVO;
import cn.iocoder.devlop.module.annual.report.socsecinfo.controller.vo.SocsecinfoOverwriteVO;
import cn.iocoder.devlop.module.annual.report.socsecinfo.convert.SocsecinfoConvert;
import cn.iocoder.devlop.module.annual.report.socsecinfo.dal.SocsecinfoMapper;
import cn.iocoder.devlop.module.annual.report.socsecinfo.dal.dataobject.SocsecinfoDO;
import cn.iocoder.devlop.module.annual.report.socsecinfo.service.SocsecinfoService;
import cn.iocoder.devlop.module.annual.report.utils.IdGenerateUtil;
import cn.iocoder.devlop.module.annual.report.utils.ObjectComparisonUtil;
import cn.iocoder.devlop.module.annual.report.utils.ReportYearHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 社保信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class SocsecinfoServiceImpl implements SocsecinfoService {

    @Resource
    private SocsecinfoMapper socsecinfoMapper;

    @Resource
    private BaseinfoService baseinfoService;

    @Resource
    private MarpripinfoService marpripinfoService;

    @Resource
    private EnttypeEntclassifyService enttypeEntclassifyService;

    @Override
    public List<SocsecinfoDO> getSocsecinfoList(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        return socsecinfoMapper.selectListByAncheid(ancheid);
    }

    @Override
    public void deleteSocsecinfoAll(String regno, String ancheyear) {
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        LambdaQueryWrapperX<SocsecinfoDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(SocsecinfoDO::getAncheid,ancheid);
        socsecinfoMapper.delete(wrapperX);
    }

    @Override
    public void createSocsecinfoByList(String regno, String ancheyear, List<SocsecinfoCreateReqVO> socsecinfoList) {
        String entclassify = enttypeEntclassifyService.getEntclassify(marpripinfoService.getMarpripinfo(regno).getEnttype());
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        if (CollUtil.isNotEmpty(socsecinfoList)) {
            List<SocsecinfoDO> socsecinfoDOS = SocsecinfoConvert.INSTANCE.convertList01(socsecinfoList);
            for (SocsecinfoDO socsecinfo : socsecinfoDOS) {
                socsecinfo.setAncheid(ancheid);
                socsecinfo.setSoseid(IdUtil.simpleUUID());
                socsecinfo.setEntclassify(entclassify);
            }
            socsecinfoMapper.insertBatch(socsecinfoDOS);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void overwriteSocsecinfo(SocsecinfoOverwriteVO updateReqVO) {
        BaseinfoDO baseinfo = baseinfoService.getBaseinfo(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        if (ObjUtil.isNotEmpty(baseinfo)){
            if (StrUtil.equals(baseinfo.getState(),"02") ||StrUtil.equals(baseinfo.getState(),"03")) {
                List<SocsecinfoDO> oldSocsecinfoList = getSocsecinfoList(updateReqVO.getRegno(), updateReqVO.getAncheyear());
                List<SocsecinfoCreateReqVO> newAlterstockinfoList = updateReqVO.getSocsecinfoList();
                List<SocsecinfoCreateReqVO> oldreqs = SocsecinfoConvert.INSTANCE.convertList3(oldSocsecinfoList);
                ObjectComparisonUtil.compareObjectList(oldreqs,newAlterstockinfoList,IdGenerateUtil.getAncheid(updateReqVO.getRegno(), updateReqVO.getAncheyear()),"股权变更");
            }
        }
        deleteSocsecinfoAll(updateReqVO.getRegno(),updateReqVO.getAncheyear());
        createSocsecinfoByList(updateReqVO.getRegno(), updateReqVO.getAncheyear(), updateReqVO.getSocsecinfoList());
        baseinfoService.updateState(updateReqVO.getRegno(), updateReqVO.getAncheyear());
    }
}
